package AssertExample;

import javax.swing.*;
class Diamond {
    static void printDiamond(int size) {
        StringBuilder diamond = new StringBuilder();
        /* print upper triangle */
        for (int r = 1, a = 1; r <= size; r++, a+=2) {
            /* print spaces */
            diamond.append(" ".repeat(Math.max(0, size - r)));
            /* print *'s */
            diamond.append("*".repeat(Math.max(0, a)));
            diamond.append("\n");
        }
        /* print lower triangle */
        for (int r = size - 1, a = 2*(size-1)-1; r >= 1; r--, a-=2) {
            /* print spaces */
            diamond.append(" ".repeat(Math.max(0, size - r)));
            /* print *'s */
            diamond.append("*".repeat(Math.max(0, a)));
            diamond.append("\n");
        }
        JOptionPane.showMessageDialog(null, diamond.toString());
        System.out.println(diamond);
    }
    public static void main(String[] args) {
        String strSize;
// Get the size of the diamond to draw
        strSize = JOptionPane.showInputDialog(null, "Enter diamond size:");
        int size = Integer.parseInt(strSize);
// Assert that the value entered is greater than 0, otherwise,
// it will generate AssertionError exception.
        try {
            assert(size > 0);
            assert(size <20);
            printDiamond(size);
        } catch (AssertionError e) {
            JOptionPane.showMessageDialog(null, "AssertionError is captured: Size should be > 0.");
        }
        System.exit(0);
    }
}